package com.wang.service;

import com.wang.mapper.BrandMapper;
import com.wang.pojo.Brand;
import com.wang.pojo.PageBean;
import com.wang.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    //创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public List<Brand> selectAll() {
       //获取SqlSession对象
        SqlSession sqlSession=factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //调用方法
        List<Brand> brands = mapper.SelectAll();

        //释放资源
        sqlSession.close();

        return brands;
    }

    @Override
    public void add(Brand brand) {
        //获取SqlSession对象
        SqlSession sqlSession=factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //调用方法
        mapper.add(brand);
        sqlSession.commit();//提交事务

        //释放资源
        sqlSession.close();
    }

    @Override
    public void update(Brand brand) {
        //获取SqlSession对象
        SqlSession sqlSession=factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //调用方法
        mapper.update(brand);
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }

    @Override
    public void delete(int id) {
        //获取SqlSession对象
        SqlSession sqlSession=factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //调用方法
        mapper.delete(id);
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids) {
        //获取SqlSession对象
        SqlSession sqlSession=factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //调用方法
        mapper.deleteByIds(ids);
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //获取SqlSession对象
        SqlSession sqlSession=factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //计算开始索引和查询的条目数
        int begin=(currentPage - 1)*pageSize;
        int size=pageSize;

        //查询当前页数据
        List<Brand> rows = mapper.seleteByPage(begin, size);

        //查询总记录数
        int totalCount = mapper.selectTotalCount();

        //封装PageBean对象
        PageBean<Brand> pageBean =new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        //释放资源
        sqlSession.close();

        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //获取SqlSession对象
        SqlSession sqlSession=factory.openSession();
        //获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //计算开始索引和查询的条目数
        int begin=(currentPage - 1)*pageSize;
        int size=pageSize;

        //处理brand条件，模糊表达
        String brandName = brand.getBrandName();
        if (brandName !=null && brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
        String companyName = brand.getCompanyName();
        if (companyName !=null && companyName.length()>0){
            brand.setCompanyName("%"+companyName+"%");
        }


        //查询当前页数据
        List<Brand> rows = mapper.seleteByPageAndCondition(begin,size,brand);

        //查询总记录数
        int totalCount = mapper.selectTotalCountAndCondition(brand);

        //封装PageBean对象
        PageBean<Brand> pageBean =new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        //释放资源
        sqlSession.close();

        return pageBean;
    }

}
